﻿using System;
using System.Configuration;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Wicresoft.SelfService.BL;
using Wicresoft.SelfService.DAO;
namespace Wicresoft.SelfService.Portal.Controllers
{
    public class WorkListController : BaseController
    {
        //
        // GET: /WorkList/
        public workListHelper myWorkListHelper;
        public ITServiceHelper myITServiceHelper;
        protected override void Initialize(RequestContext requestContext)
        {
            if (myWorkListHelper == null) myWorkListHelper = new workListHelper();
            if (myITServiceHelper == null) myITServiceHelper = new ITServiceHelper();
            base.Initialize(requestContext);
        }
        [Authorize]
        public ActionResult Index()
        {
            //System.Web.HttpContext.Current.User.Identity.Name
            WorkListCounter myCounter = myWorkListHelper.getWorkListCounter(System.Web.HttpContext.Current.User.Identity.Name);
            IDictionary<string, int> counts = new Dictionary<string, int>();
            counts.Add("ToDo", myCounter.WorkList_Schedule);
            counts.Add("Done", myCounter.WorkList_Done);
            counts.Add("Draft", myCounter.WorkList_Draft);
            int c = myCounter.MyWorkList.finished + myCounter.MyWorkList.processing + myCounter.MyWorkList.waiting;
            counts.Add("Apply", c);
            counts.Add("ApplyFin", myCounter.MyWorkList.finished);
            counts.Add("ApplyProc", myCounter.MyWorkList.processing);
            counts.Add("ApplyWait", myCounter.MyWorkList.waiting);
            return View(counts);
        }
        [Authorize]
        public ActionResult ScheduledWorkList()
        {
            string UserID = System.Web.HttpContext.Current.User.Identity.Name;
            int ps = 5;
            int pi = 1;
            List<ServiceDetail> TodoList = myWorkListHelper.getTodoList(UserID, null, string.Empty, string.Empty, ps, pi, null, null, null);
            ViewData["count"] = myWorkListHelper.getTodoListCount(UserID, null, string.Empty, string.Empty, null, null, string.Empty);
            ViewData["size"] = ps;
            ViewData["index"] = pi;
            return View(TodoList);
        }
        [AcceptVerbs(HttpVerbs.Get)]
        public JsonResult getToDoList_By_params(int? step, string requestID, string userName, Nullable<int> ps, Nullable<int> pi, DateTime? startTime, DateTime? endTime, string description, int serviceTypeID)
        {
            string UserID = System.Web.HttpContext.Current.User.Identity.Name;
            List<ServiceDetail> myToDoList = null;
            int listcount = 0;
            do
            {
                myToDoList = myWorkListHelper.getTodoList(UserID, serviceTypeID, requestID, userName, ps, pi, startTime, endTime, description);
                listcount = myWorkListHelper.getTodoListCount(UserID, serviceTypeID, requestID, userName, startTime, endTime, description);
                pi--;
            }
            while (pi > 0 && myToDoList.Count == 0);
            JsonResult json = new JsonResult
        {
            Data = new
            {
                list = myToDoList,
                count = listcount
            }
        };
            return Json(json, JsonRequestBehavior.AllowGet);
        }
        [AcceptVerbs(HttpVerbs.Get)]
        public JsonResult getAllServiceList()
        {
            List<ServiceList> serviceList = myITServiceHelper.getAllServiceList();
            foreach (ServiceList sl in serviceList)
            {
                if (sl.serviceName == "其他")
                {
                    ServiceList parent = this.myITServiceHelper.getServiceList_By_ID(sl.serviceID);
                    if (parent != null) sl.serviceName = string.Concat(parent.serviceName, "-", sl.serviceName);
                }
            }
            return Json(serviceList, JsonRequestBehavior.AllowGet);
        }
        [Authorize]
        public ActionResult WorklistHistory()
        {
            string UserID = System.Web.HttpContext.Current.User.Identity.Name;
            int ps = 5;
            int pi = 1;
            List<ServiceDetail> DoneList = myWorkListHelper.getDoneList(UserID, string.Empty, null, null, null, string.Empty, null, null, ps, pi);
            ViewData["count"] = myWorkListHelper.getDoneListCount(UserID, string.Empty, string.Empty, null, null, string.Empty, null, null);
            ViewData["size"] = ps;
            ViewData["index"] = pi;
            return View(DoneList);
        }
        [AcceptVerbs(HttpVerbs.Get)]
        public JsonResult getDoneList_By_params(string requestID, string description, Nullable<int> ServiceTypeID, int status, string userName, DateTime? startTime, DateTime? endTime, Nullable<int> ps, Nullable<int> pi, int serviceTypeID)
        {
            string UserID = System.Web.HttpContext.Current.User.Identity.Name;
            List<ServiceDetail> myDoneWorkList = myWorkListHelper.getDoneList(UserID, requestID, description, serviceTypeID, status, userName, startTime, endTime, ps, pi);
            int listcount = myWorkListHelper.getDoneListCount(UserID, requestID, description, ServiceTypeID, status, userName, startTime, endTime);
            JsonResult json = new JsonResult
            {
                Data = new
                {
                    list = myDoneWorkList,
                    count = listcount
                }
            };
            return Json(json, JsonRequestBehavior.AllowGet);
        }
        [Authorize]
        public ActionResult MyDraft()
        {
            string UserID = System.Web.HttpContext.Current.User.Identity.Name;
            int ps = 5;
            int pi = 1;
            List<ServiceDetail> myDraftList = myWorkListHelper.getDraftList(UserID, string.Empty, string.Empty, null, null, ps, pi);
            ViewData["count"] = myWorkListHelper.getDraftListCount(UserID, string.Empty, string.Empty, null, null);
            ViewData["size"] = ps;
            ViewData["index"] = pi;
            return View(myDraftList);
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public JsonResult getMyDraftList_By_params(string requestID, string description, Nullable<DateTime> startTime, Nullable<DateTime> endTime, Nullable<int> ps, Nullable<int> pi)
        {
            string UserID = System.Web.HttpContext.Current.User.Identity.Name;
            List<ServiceDetail> myDraftList = myWorkListHelper.getDraftList(UserID, requestID, description, startTime, endTime, ps, pi);
            int listcount = myWorkListHelper.getDraftListCount(UserID, requestID, description, startTime, endTime);
            JsonResult json = new JsonResult
            {
                Data = new
                {
                    list = myDraftList,
                    count = listcount
                }
            };
            return Json(json, JsonRequestBehavior.AllowGet);
        }
        [Authorize]
        public ActionResult MyWorklist()
        {
            int? status = null;
            string statusStr = Request.QueryString["status"];
            if (!string.IsNullOrEmpty(statusStr)) status = int.Parse(statusStr);
            string UserID = System.Web.HttpContext.Current.User.Identity.Name;
            int ps = 5;

            int pi = 1;
            List<ServiceDetail> myWorklist = myWorkListHelper.getMyApplyList(UserID, 1, string.Empty, string.Empty, status, null, null, ps, pi);

            List<StatusSelectList> selectList = new List<StatusSelectList>();
            selectList.Add(new StatusSelectList { textField = "--全部--", value = 0 });
            selectList.Add(new StatusSelectList { textField = "审批中", value = 1 });
            selectList.Add(new StatusSelectList { textField = "等待中", value = 2 });
            selectList.Add(new StatusSelectList { textField = "处理中", value = 3 });
            selectList.Add(new StatusSelectList { textField = "已完成", value = 4 });
            selectList.Add(new StatusSelectList { textField = "已拒绝", value = 5 });
            selectList.Add(new StatusSelectList { textField = "已取消", value = 6 });

            ViewData["statusList"] = new SelectList(selectList, "value", "textField", status);
            ViewData["count"] = myWorkListHelper.getMyApplyListCount(UserID, 1, string.Empty, string.Empty, status, null, null);

            ViewData["size"] = ps;
            ViewData["index"] = pi;
            return View(myWorklist);
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public JsonResult getMyWorkList_By_params(string requestID, string description, string status, Nullable<DateTime> startTime, Nullable<DateTime> endTime, Nullable<int> ps, Nullable<int> pi)
        {
            int mySubmitType = 1;
            Nullable<int> my_Status = Convert.ToInt32(status);
            string UserID = System.Web.HttpContext.Current.User.Identity.Name;
            //if (mySubmitType == 2)
            //{
            //    my_Status = my_Status - 1;
            //}
            if (my_Status <= 0)
            {
                my_Status = null;
            }

            int listcount = myWorkListHelper.getMyApplyListCount(UserID, mySubmitType, requestID, description, my_Status, startTime, endTime);
            List<ServiceDetail> myWorkList = myWorkListHelper.getMyApplyList(UserID, mySubmitType, requestID, description, my_Status, startTime, endTime, ps, pi);
            JsonResult json = new JsonResult
            {
                Data = new
                {
                    list = myWorkList,
                    count = listcount
                }
            };
            return Json(json, JsonRequestBehavior.AllowGet);
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public JsonResult GetStatusFromEnum(string val)
        {
            int Val = Convert.ToInt32(val);
            List<string> MyStatusList;
            if (Val == 1)
            {
                MyStatusList = Enum.GetNames(typeof(Wicresoft.SelfService.DAO.serviceStatusView)).ToList<string>();
            }
            else
            {
                MyStatusList = Enum.GetNames(typeof(Wicresoft.SelfService.DAO.ITSMStatusView)).ToList<string>();
            }
            return Json(MyStatusList, JsonRequestBehavior.AllowGet);

        }


        [Authorize]
        public ActionResult MyApply()
        {
            return View();
        }

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
            if (myITServiceHelper != null)
                myITServiceHelper.Dispose();
        }

    }
}

